package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.commons.vo.UmsMemberVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.FmsMemberWalletVo;
import com.quan.mall.entity.FmsMemberWallet;
import com.quan.mall.mapper.FmsMemberWalletMapper;
import com.quan.mall.service.FmsMemberWalletService;


@Slf4j
@Service
public class FmsMemberWalletServiceImpl extends MyBaseServiceImpl<FmsMemberWalletMapper, FmsMemberWallet> implements FmsMemberWalletService {

    @Autowired
	private FmsMemberWalletMapper mapper;

	@Override
    public List<FmsMemberWalletVo> queryList(PageUtils pageUtils, FmsMemberWalletVo vo) {
        QueryWrapper<FmsMemberWallet> queryWrapper = new QueryWrapper<FmsMemberWallet>(vo);

        List<FmsMemberWallet> list = super.list(pageUtils, queryWrapper);
        List<FmsMemberWalletVo> collect = list.stream().map(item -> {
            FmsMemberWalletVo bean = new FmsMemberWalletVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, FmsMemberWalletVo vo) {
        QueryWrapper<FmsMemberWallet> queryWrapper = new QueryWrapper<FmsMemberWallet>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword())
                .or().like("phone", vo.getPhone());
            });
        }

        // 总金额 min 大于 0
        if (null != vo.getTotalAmountMin() && vo.getTotalAmountMin().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.ge("total_amount",vo.getTotalAmountMin());
        }

        // 总金额 max 大于 0
        if (null != vo.getTotalAmountMin() && vo.getTotalAmountMax().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.le("total_amount",vo.getTotalAmountMax());
        }

        // 可提现金额 min 大于 0
        if (null != vo.getTakeAmountMin() && vo.getTakeAmountMin().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.ge("take_amount",vo.getTakeAmountMin());
        }

        // 可提现金额 max 大于 0
        if (null != vo.getTakeAmountMin() && vo.getTakeAmountMax().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.le("take_amount",vo.getTakeAmountMax());
        }

        // 待入账金额 min 大于 0
        if (null != vo.getFreezeAmountMin() && vo.getFreezeAmountMin().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.ge("freeze_amount",vo.getFreezeAmountMin());
        }

        // 待入账金额 max 大于 0
        if (null != vo.getFreezeAmountMin() && vo.getFreezeAmountMax().compareTo(BigDecimal.ZERO) == 1) {
            queryWrapper.le("freeze_amount",vo.getFreezeAmountMax());
        }

        final IPage<FmsMemberWallet> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<FmsMemberWallet> records = page.getRecords();

        final List<FmsMemberWallet> collect = records.stream().map((item) -> {
            FmsMemberWalletVo bean = new FmsMemberWalletVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public FmsMemberWallet getMemberWallter(Long memberId) {
	    QueryWrapper<FmsMemberWallet> queryWrapper = new QueryWrapper<FmsMemberWallet>();
	    queryWrapper.eq("member_id", memberId);
	    return super.getOne(queryWrapper);
    }

}